package com.malinkang.sqlite;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by malinkang on 16/3/28.
 */
public class UserAdapter extends CursorAdapter {

    private LayoutInflater mLayoutInflater;


    public UserAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
        init(context);
    }

    public UserAdapter(Context context, Cursor c) {
        super(context, c, FLAG_AUTO_REQUERY);
        init(context);
    }

    private void init(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = mLayoutInflater.inflate(R.layout.item_user, parent, false);
        new ViewHolder(view);
        return view;
    }

    @Override
    public void bindView(View view, final Context context, final Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        String name = cursor.getString(cursor.getColumnIndex(DBOpenHelper.COLUMN_USERNAME));
        String phone = cursor.getString(cursor.getColumnIndex(DBOpenHelper.COLUMN_PHONE));
        String avatar = cursor.getString(cursor.getColumnIndex(DBOpenHelper.COLUMN_AVATAR));
        final Integer id = cursor.getInt(cursor.getColumnIndex(BaseColumns._ID));
        final User user = new User();
        user.setUsername(name);
        user.setPhone(phone);
        user.setAvatar(avatar);
        holder.mUsernameTextView.setText("用户名:"+name);
        holder.mPhoneTextView.setText("电话："+phone);
        holder.mAvatarImageView.setImageURI(Uri.parse(avatar));

        holder.mEditImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, InsertOrUpdateActivity.class);
                intent.putExtra("user",user);
                context.startActivity(intent);
            }
        });
        holder.mDeleteImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.getContentResolver().delete(Uri.parse("content://" + UserContentProvider.sAuthority + "/" + DBOpenHelper.USER_TABLE_NAME)
                        , BaseColumns._ID + "=?", new String[]{id+""});
            }
        });
    }

    static class ViewHolder {
        @Bind(R.id.iv_imageview) CircleImageView mAvatarImageView;
        @Bind(R.id.tv_username) TextView mUsernameTextView;
        @Bind(R.id.tv_phone) TextView mPhoneTextView;
        @Bind(R.id.iv_edit) ImageView mEditImageView;
        @Bind(R.id.iv_delete) ImageView mDeleteImageView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setTag(this);
        }
    }
}
